package com.woniuxy.patrol.controller;

import com.woniuxy.common.utils.ResponseUtil;
import com.woniuxy.patrol.service.MinioService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/patrol/upload")
public class UploadController {

    @Resource
    private MinioService minioService;

    /**
     * 前端上传图片到该接口，返回MinIO的URL
     */
    @PostMapping("/image")
    public ResponseUtil uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            // 调用MinIO服务上传，获取短URL
            String url = minioService.uploadImage(file);
            return ResponseUtil.success(url); // 返回格式：{code:0, data: "http://..."}
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("图片上传失败：" + e.getMessage());
        }
    }
}